import Vue from 'vue'
import VueResource from 'vue-resource'
import store from '@/store'

Vue.use(VueResource)

// HTTP相关
Vue.http.options.crossOrigin = true
Vue.http.options.credentials = true

Vue.http.interceptors.push((request, next) => {
  // 这里对请求体进行处理
  request.headers = request.headers || {}
  const token = sessionStorage.getItem('token')
  if (!token && request.url !== '/api/login') {
    store.commit('logout')
  }
  request.headers.set('access_token', token)

  next((response) => {
    // 这里可以对响应的结果进行处理
    if (response.body.status === 1003 || response.body.status === 1004) {
      // 跳转到登录页面
      // VueCookie.delete('access_token')
      // let fromUrl = window.location.href
      // Vue.prototype.nativeHandler.login(fromUrl)
    }
  })
  // next()
})

const API_ROOT = '/api/service'
export const DesignerResource = Vue.resource(API_ROOT + '/designerService/{id}')
export const ConsultProductResource = Vue.resource(API_ROOT + '/consultProductService/{id}')
